<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>Lånere</title>
<link rel="stylesheet" type="text/css" href="../CSS/CSS.css" />
<link rel="stylesheet" type="text/css" href="../CSS/header.css" />
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
if (!window.jQuery) {
    document.write('<script src="../Javascript/jQuery.js"><\/script>');
}
</script>
<script src="../Javascript/Popups.js"></script>
</head>
<body>

<?php 
    include_once("database.php");
    include_once("session_start.php");
    include_once 'admin_check.php';    
    include_once 'header.php';
?>
        
<h1>Lånere</h1>
<?php include 'get_message.php' ?> 
<?php
    $query = "SELECT UserName, FirstName, LastName, users.UserId 
              FROM users,borroweditem
              WHERE users.UserId = borroweditem.UserId
              GROUP BY users.UserId";
    $userselect = $db -> query($query);
    while($user = $userselect -> fetch_assoc()){
        $url = "";
        if(isset($_GET["show"])){
            foreach ($_GET["show"] as $key => $value) {
                $url .= "&show[]=".$value;
            }
        }        
        echo'<div>
                <a href="user.php?id='.$user["UserId"].'">'.$user["UserName"].'</a> ';
                
            if(isset($_GET["show"])&&in_array($user["UserId"],$_GET["show"])){
                $url = "";
                $counter = 1;
                foreach ($_GET["show"] as $value) {
                    $url .= $value==$user["UserId"]? "":"show[]=".$value;
                    if(count($_GET["show"])<$counter){
                        $url .='&';
                    }
                    $counter++;
                }
                echo'<a href="loaners.php?'.$url.'">Skjul materialer</a>';
            }else{                
                echo'<a href="loaners.php?show[]='.$user["UserId"].$url.'">Vis materialer</a>';
            }
        
        echo'</div>';
        
        if(isset($_GET["show"])&&in_array($user["UserId"], $_GET["show"])){
            echo '<table border="1">
                    <tr>
                        <th>
                            Billede
                        </th>
                        <th>
                            Navn
                        </th>
                        <th>
                            Medie
                        </th>
                        <th>
                            Genre
                        </th>
                        <th>
                            Udgivelses dato
                        </th>
                    </tr>';
                        $query = "SELECT MaterialId FROM borroweditem WHERE UserId=".$user["UserId"];
                        $itemsselect= $db -> query($query);
                        while($items = $itemsselect-> fetch_assoc()){
                            $_GET["id"]=$items["MaterialId"];
                            include 'items.php';
                        }
                
            echo '</table>';
        }
    }

?>
<?php include 'footer.php' ?>
</body>
</html>
